package com.analyse.stock.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.analyse.stock.model.StockInfoModel;
import com.analyse.stock.service.IStockInfo;
import com.analyse.stock.service.IStockKlineService;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class StockKlineService implements IStockKlineService {
    @Autowired
    private StockKlineDailyService stockKlineDailyService;

    @Autowired
    private StockKlineWeeklyService stockKlineWeeklyService;

    @Autowired
    private StockKlineMonthlyService stockKlineMonthlyService;

    @Autowired
    private StockKlineYearlyService stockKlineYearlyService;

    @Autowired
    private IStockInfo istockInfo;

    @Override
    public Integer getStockKlineHistoryByEastMoney(String stockCode) {
        Set<String> sets = Sets.newHashSet();
        if(StringUtils.isNotBlank(stockCode)){
            sets.add(stockCode);
        }
        List<StockInfoModel> stockList = istockInfo.getStockList(sets);
        if(CollUtil.isEmpty(stockList)){
            return 0;
        }
        /**
         * 获取某一只股票的日K线
         */
        try{
            Integer num1 = stockKlineDailyService.pullStockKlineDailyHistoryByEastMoney(stockList);
            log.info("### 获取日K线,共计：{}条",num1);
        }catch (Exception ex){
            log.error("### 获取日K线,异常error：{}",ex.getMessage(),ex);
        }

        /**
         * 获取某一只股票的周K线
         */
        try{
            Integer num2 = stockKlineWeeklyService.pullStockKlineWeeklyHistoryByEastMoney(stockList);
            log.info("### 获取周K线,共计：{}条",num2);
        }catch (Exception ex){
            log.error("### 获取周K线,异常error：{}",ex.getMessage(),ex);
        }

        /**
         * 获取某一只股票的月K线
         */
        try{
            Integer num3 = stockKlineMonthlyService.pullStockKlineMonthlyHistoryByEastMoney(stockList);
            log.info("### 获取月K线,共计：{}条",num3);
        }catch (Exception ex){
            log.error("### 获取月K线,异常error：{}",ex.getMessage(),ex);
        }

        /**
         * 获取某一只股票的年K线
         */
        try{
            //Integer num4 = stockKlineYearlyService.getStockKlineYearlyHistoryByEastMoney(stockList);
            //log.info("### 获取年K线,共计：{}条",num4);
        }catch (Exception ex){
            log.error("### 获取年K线,异常error：{}",ex.getMessage(),ex);
        }
        return 0;
    }

    /**
     * 同步日线级别数据
     */
    @Override
    public Integer getLatest(String stockCode,String date) {
        Set<String> sets = Sets.newHashSet();
        if(StringUtils.isNotBlank(stockCode)){
            sets.add(stockCode);
        }
        List<StockInfoModel> stockList = istockInfo.getStockList(sets);
        if(CollUtil.isEmpty(stockList)){
            return 0;
        }
        return stockKlineDailyService.pullLatest(stockList,date);
    }
}
